﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;
using System.Text;
using System.Data;
using MxWeiXinPF.Common.log;

namespace MxWeiXinPF.Web.admin.wxapp
{
    public partial class wxappverifylist : Web.UI.ManagePage
    {

        protected int totalCount;
        protected int page;
        protected int pageSize;

        BLL.wxapp_userList auBll = new BLL.wxapp_userList();

        protected string keywords = string.Empty;
        protected int auStatus = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            this.keywords = MXRequest.GetQueryString("keywords");
            this.auStatus = MyCommFun.RequestInt("auStatus", -1);

            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {

                RptBind(CombSqlTxt(auStatus,keywords), "status asc, applyTime desc,id desc");
            }
        }

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {

            this.page = MXRequest.GetQueryInt("page", 1);
            txtKeywords.Text = this.keywords;
            ddlauStatus.SelectedValue = this.auStatus.ToString();

            DataSet ds = auBll.GetModelByUserApp(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr;
              
                int count = ds.Tables[0].Rows.Count;
                for (int i = 0; i < count; i++)
                {
                    dr = ds.Tables[0].Rows[i];
                    //状态（0申请中，1审核通过，2审核失败）
                    int status = MyCommFun.Obj2Int(dr["austatus"].ToString());
                    int buyCount = MyCommFun.Obj2Int(dr["buyCount"]);
                    DateTime auvalidDate = MyCommFun.Obj2DateTime(dr["auvalidDate"],DateTime.Parse("1990-1-1"));

                    if (status == 0)
                    {
                        dr["status_s"] = "<span class=\"act_before\">申请中</span>";
                        dr["caozuo"] = "<a href='wxappverifyDetail.aspx?auid=" + dr["auid"]+"'>审核</a>";
                    }
                    else if (status == 1)
                    {
                        dr["status_s"] = "<span class=\"act_end\">已开通</span>";
                        if (auvalidDate != DateTime.Parse("1990-1-1") && auvalidDate < DateTime.Now)
                        {
                            dr["status_s"] = "<span class=\"act_end\">已过期</span>";
                            dr["caozuo"] = "<a href='wxappverifyDetail.aspx?auid=" + dr["auid"] + "'>延长时间</a>";
                        }
                        else
                        {
                            dr["caozuo"] = "<a href='wxappverifyDetail.aspx?auid=" + dr["auid"] + "'>查看</a>";
                        }
                    }
                    else if (status == 2)
                    {
                        dr["status_s"] = "<span class=\"act_end\">审核失败</span>";
                        dr["caozuo"] = "<a href='wxappverifyDetail.aspx?auid=" + dr["auid"] + "'>审核</a>";
                    }


                    if (buyCount == 1)
                    {
                        dr["buyinfo"] = "新购";
                    }
                    else
                    {
                        dr["buyinfo"] = "续约";
                    }


                }
                ds.AcceptChanges();
            }


            this.rptList.DataSource = ds;
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("wxappverifylist.aspx", "austatus={0}&keywords={1}&page={2}", this.auStatus.ToString(), this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(int auStatus, string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and  wxappName like  '%" + _keywords + "%' or    user_name like '%" + _keywords + "%' or storeName.like  '%" + _keywords + "%' ");
            }
            //状态-1表示所有状态
            if (auStatus != -1)
            {
                strTemp.Append("  and u.status= "+auStatus);
            }
            return strTemp.ToString();
        }
        #endregion

        #region 返回每页数量=============================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("wxappverifylist_page_size"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("wxappverifylist.aspx", "austatus={0}&keywords={1}",this.ddlauStatus.SelectedItem.Value, txtKeywords.Text));
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("wxappverifylist_page_size", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("wxappverifylist.aspx", "austatus={0}&keywords={1}", this.auStatus.ToString(), this.keywords));
        }

        protected void ddlauStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("wxappverifylist.aspx", "austatus={0}&keywords={1}", this.ddlauStatus.SelectedItem.Value, txtKeywords.Text));
        }
    }
}